package com.lvandroid.play.player;

import android.app.Activity;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;

import com.github.barteksc.pdfviewer.PDFView;
import com.github.barteksc.pdfviewer.listener.OnLoadCompleteListener;
import com.github.barteksc.pdfviewer.listener.OnPageChangeListener;
import com.github.barteksc.pdfviewer.listener.OnPageErrorListener;
import com.github.barteksc.pdfviewer.scroll.DefaultScrollHandle;
import com.lvandroid.play.PlayerApplication;
import com.lvandroid.play.bean.PDFItem;
import com.lvandroid.play.util.LogUtil;
import com.lvandroid.player.R;

import java.io.File;
import java.io.FileInputStream;
import java.util.List;

public class PDFPlayer extends AllRoundPlayer<ViewGroup, PDFItem>
        implements OnPageChangeListener,
        OnLoadCompleteListener, OnPageErrorListener {
    private static final String TAG = "PDFPlayer";
    private PDFView mPdfView;

    public PDFPlayer() {
        mPlayItem = new PDFItem();
    }

    @Override
    public void setDataSource(List<String> source) {
        if(null == source || source.size() < 1)
            throw new IllegalArgumentException("setDataSource error!");
        setDataSource(source.get(0));
    }

    @Override
    public void setDataSource(String source) {
        mPlayItem.url = source;
    }

    @Override
    public void setDisplay(ViewGroup v) {
        mDisplay = v;
        initDisplayInternal();
    }

    private void initDisplayInternal() {
        View v = PlayerApplication.getLayoutInflater().inflate(R.layout.layout_pdf, null);
        mPdfView = v.findViewById(R.id.pdfView);
        mDisplay.removeAllViews();
        mDisplay.addView(v);
    }

    @Override
    public void start(List<String> source, ViewGroup display) {
        setDisplay(display);
        setDataSource(source);
        start();
    }

    @Override
    public void start(String source) {
        setDataSource(source);
        start();
    }

    @Override
    public void start() {
        if(null == mDisplay) {
            LogUtil.e(TAG, "Please call display method before start.");
            return;
        }
        startInternal();
    }

    private void startInternal() {
        try {
            LogUtil.i(TAG, "source : " + mPlayItem.url);
            //mPdfView.fromAsset("Kotlin.pdf")
            mPdfView.fromFile(new File(mPlayItem.getSource()))
                    .defaultPage(0)
                    .onPageChange(this)
                    .enableSwipe(false)
                    .enableAnnotationRendering(true)
                    .onLoad(this)
                    .scrollHandle(new DefaultScrollHandle(PlayerApplication.getGlobalContext()))
                    .spacing(10) // in dp
                    .onPageError(this)
                    .onPageChange(this)
                    .load();
        } catch (Exception e) {
            LogUtil.e(TAG, "load pdf file error!");
            e.printStackTrace();
        }
    }

    @Override
    public void loadComplete(int nbPages) {
        LogUtil.i(TAG, "loadComplete : " + nbPages);
    }

    @Override
    public void onPageChanged(int page, int pageCount) {
        LogUtil.i(TAG, "onPageChanged : " + page + ", " + pageCount);
    }

    @Override
    public void onPageError(int page, Throwable t) {
        LogUtil.e(TAG, "onPageError : " + page);
        t.printStackTrace();
    }
}
